package com.liser.southernstar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.os.Environment;

import androidx.multidex.MultiDexApplication;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.liser.print.bluetooth.BluetoothMonitor;
import com.liser.print.bluetooth.BtManager;
import com.liser.southernstar.app.AppUpgradeHelper;
import com.liser.southernstar.net.Network;
import com.xuexiang.xutil.XUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * Created by: var_rain.
 * Created date: 2018/10/16.
 * Description: 程序入口点
 */
public class MainApplication extends MultiDexApplication implements Application.ActivityLifecycleCallbacks {

    /* 静态实例 */
    @SuppressLint("StaticFieldLeak")
    private static MainApplication instance;

    /* 当前所在的界面 */
    private Activity activity;

    /* Activity管理集合 */
    private List<Activity> activities;

    @Override
    public void onCreate() {
        super.onCreate();

        // 应用程序上下文
        instance = this;

        // 创建Activity储存管理
        activities = new ArrayList<>();

        // 注册Activity生命周期回调
        registerActivityLifecycleCallbacks(this);

        // 初始化网络请求
        Network.init();

        XUtil.init(this);

        ImagePipelineConfig frescoConfig = ImagePipelineConfig.newBuilder(this).setDownsampleEnabled(true).build();
        Fresco.initialize(this, frescoConfig);

        // 经典蓝牙初始化
        BtManager.getInstance()
                .init(this);

        // 应用升级
        appUpdate();
    }

    private void appUpdate() {
        AppUpgradeHelper.get()
                .time(30, TimeUnit.SECONDS)
                .apkVersion(BuildConfig.VERSION_CODE, BuildConfig.APPLICATION_ID)
                .apkStoragePath(Environment.getExternalStorageDirectory() + BuildConfig.APK_STORAGE_PATH)
                .providerName(BuildConfig.APPLICATION_ID + ".fileprovider")
                .application(this)
                .build();
    }

    /**
     * 获取静态实例
     *
     * @return 返回一个MassApplication对象
     */
    public static MainApplication instance() {
        return instance;
    }

    /**
     * 获取当前显示的Activity
     *
     * @return 返回一个 {@link Activity} 对象
     */
    public Activity activity() {
        return this.activity;
    }

    /**
     * 退出APP
     */
    public void quit() {
        // 移除蓝牙连接监听
        BluetoothMonitor.remove(this);

        // 清空所有Activity
        for (Activity act : activities) {
            if (act != null && !act.isFinishing()) {
                act.finish();
            }
        }
        activities.clear();

        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        activities.add(activity);
        this.activity = activity;
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        activities.remove(activity);
    }
}
